<template>
  <div>
    <div class="detail_box">
      <div class="operate_box">
        <span class="operate_btn search_btn" @click="synData">同步数据</span>
      </div>
      <div class="table_box">
        <el-table
          border
          v-loading="loading"
          :header-cell-style="tableHeader"
          ref="multipleTable"
          :data="tableInfo"
          tooltip-effect="dark"
          style="width: 100%;">
          <el-table-column
            width="85"
            prop="recordcode"
            label="ID">
          </el-table-column>
          <el-table-column
            prop="modifytime"
            sortable
            label="修改时间">
            <template slot-scope="scope">{{ formatTime(scope.row.modifytime) }}</template>
          </el-table-column>
          <el-table-column
            width="300"
            label="楼名">
            <template slot-scope="scope">
              <span>{{ scope.row.buildingname ? scope.row.buildingname : '——' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="buildingheight"
            sortable
            label="楼高(米)">
          </el-table-column>
          <el-table-column
            prop="firedanger"
            sortable
            label="火灾危险性">
            <template slot-scope="scope">
              <span>{{ scope.row.firedanger ? scope.row.firedanger + '级' : '——' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="fireresistantlevel"
            sortable
            label="耐火等级">
            <template slot-scope="scope">
              <span>{{ scope.row.fireresistantlevel ? scope.row.fireresistantlevel + '级' : '——' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="是否带夹层">
            <template slot-scope="scope">
              <span :style="'color:'+(scope.row.ismezzanine?'green':'#e40000')">{{ scope.row.ismezzanine ? '是' : '否' }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            v-if="btn&&btn.bindManager"
            fixed="right"
            width="140">
            <template slot-scope="scope">
              <div class="table_btn">
                <span class="change_info" @click="bindEvents(scope.row)">绑定管理员</span>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="record_page">
        <span class="page_count">共<i>{{ totals }}</i>条</span>
        <el-pagination
          @size-change="sizeChange"
          @current-change="getTableInfo"
          background
          :current-page="currentPage"
          :page-sizes="[15, 20, 30, 40]"
          :page-size="pageSize"
          layout="prev, pager, next, sizes,jumper"
          :total="totals">
        </el-pagination>
      </div>
    </div>
    <bindEvent :eid="eventId" :ids="events" :type="eType" @listenToEvent="getEventInfo" v-if="isBind"/>
  </div>
</template>

<script>
import bindEvent from "./bindEvent";
import commons from "@/utils//common";
import {getBtn} from "@/utils/btnAuth";
import {getFireBuilding, synFireBuilding} from "../../../api/building";

export default {
  components: {
    bindEvent
  },
  data() {
    return {
      tableInfo: [],
      currentPage: 1,
      pageSize: 15,
      totals: 0,
      loading: false,
      isBind: false,
      eventId: '',
      events: '',
      eType: 0,
      btn: {}
    }
  },
  mounted() {
    getBtn().then(res=>{
      this.btn = res;
      if (this.btn.query) {
        this.getTableInfo(1);
      }
    })
  },
  methods: {
    // 获取列表信息
    getTableInfo(page) {
      let that = this;
      that.loading = true;
      that.currentPage = page;
      let queryParams = {
        limit: that.pageSize,
        page: page,
      }
      getFireBuilding(queryParams).then(res => {
        that.tableInfo = res.data ? res.data.list : [];
        that.totals = res.data ? res.data.count : 0;
        that.loading = false;
      }, (err) => {
        that.loading = false;
      });
    },

    synData() {
      let that = this;
      that.$confirm('确定同步数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        that.loading = true;
        synFireBuilding().then(res => {
          if (res.success) {
            that.getTableInfo(that.currentPage);
            that.$message.success('同步成功！');
          }
          that.loading = false;
        }, (err) => {
          that.loading = false;
          that.$message.error('同步失败，请重试');
        });
      }).catch(() => {
        // console.log('fail')
      });
    },

    // 绑定
    bindEvents(obj) {
      this.eventId = obj ? obj.recordcode : '';
      this.events = obj && obj.managerIds ? obj.managerIds : '';
      this.isBind = true;
    },

    getEventInfo(e) {
      this.isBind = false;
      if (e.refresh) {
        this.getTableInfo(this.currentPage);
      }
    },

    formatTime(time) {
      return commons.formatTime(time);
    },

    sizeChange(val) {
      this.pageSize = val;
      this.getTableInfo(1)
    },

    // 设置table标题居中
    tableHeader({row, column, rowIndex, columnIndex}) {
      return 'background:#F0F2F5;color:#111;padding:10px 0;font-size:14px;font-weight:600'
    },
  }
}
</script>

<style scoped>

</style>
